package com.wuseguang.springcloud.controller;


import com.wuseguang.springcloud.Classcardservice.ClassCardService;
import com.wuseguang.springcloud.Filter.RequestParameterWrapper;
import com.wuseguang.springcloud.service.ClassNoticeService;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 班级班牌通知表 前端控制器
 * </p>
 *
 * @author
 * @since 2020-01-15
 */
@RestController
@RequestMapping("/classNotice")
public class ClassNoticeController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ClassNoticeService classNoticeServiceImpl;

    //推送接口
    @Autowired
    private ClassCardService classCardService;

    /**
     * 查询班级班牌通知
     *
     * @param request
     * @param classId 班级id
     * @param indexNo
     * @return
     */
    @RequestMapping("/findClassNoticeList")
    public Result findClassNoticeList(RequestParameterWrapper request,
                                      @RequestParam(value = "classId", required = false) String[] classId,
                                      @RequestParam(value = "indexNo", required = false, defaultValue = "1") Integer indexNo,
                                      @RequestParam(value = "rows", required = false, defaultValue = "20") Integer rows) {

        logger.info("查询班级班牌通知 ClassNoticeController - > findClassNoticeList");
        //学期id
        String termId = request.getParam("termId");
        //学校id
        String schoolId = request.getParam("schoolId");
        //管理员
        String admin = request.getParam("admin");
        //教师id
        String teacherId = request.getParam("teacherId");

        return classNoticeServiceImpl.findClassNoticeList(classId, termId, schoolId, indexNo, rows, admin, teacherId);
    }

    /**
     * 添加班级通知
     * @param request
     * @param classId 班级id
     * @param content 内容
     * @param url 图片url
     * @param show 是否展示在班牌首页 0不显示，1显示
     * @param pshow 	是否展示在家长端小程序 0不显示，1显示
     * @param state 0紧急通知，1非紧急通知
     * @param annex 附件
     * @param website 网址/网站
     * @return
     */
    @RequestMapping("/addNotice")
    public Result addNotice(RequestParameterWrapper request,
                            @RequestParam(value = "classId", required = false) String[] classId,
                            @RequestParam(value = "content", required = false) String content,
                            @RequestParam(value = "url", required = false) String url,
                            @RequestParam(value = "show", required = false, defaultValue = "0") Integer show,
                            @RequestParam(value = "pshow", required = false, defaultValue = "0") Integer pshow,
                            @RequestParam(value = "state", required = false, defaultValue = "0") Integer state,
                            @RequestParam(value = "annex", required = false) String annex,
                            @RequestParam(value = "website", required = false )String website) {

        logger.info("添加班级通知 ClassNoticeController - > addNotice");

        //学校id
        String schoolId = request.getParam("schoolId");
        //教师id
        String teacherId = request.getParam("teacherId");
        //教师名称
        String teacherName = request.getParam("teacherName");
        //学期id
        String termId = request.getParam("termId");
        Result result = classNoticeServiceImpl.addNotice(show,pshow,classId, content, url, state, termId, schoolId, teacherId, teacherName,annex,website);
        if (classId != null && classId.length > 0) {
            for (int i = 0; i < classId.length; i++) {
                classCardService.classesAndCards(classId[i]);
            }
        }
        return result;
    }

    /**
     * 删除班级通知
     *
     * @param request
     * @param id
     * @return
     */
    @RequestMapping("/deleteNotice")
    public Result deleteNotice(RequestParameterWrapper request,
                               @RequestParam(value = "id", required = false) String id) {
        //教师id
        String teacherId = request.getParam("teacherId");
        //获取是不是管理员
        String admin = request.getParam("admin");
        logger.info("删除视频  id = {}", id);

        return classNoticeServiceImpl.deleteNotice(id, teacherId, admin);
    }

}
